package queue;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * 滑动窗口的最大值
 */
public class MaxValOfSlideWindow {

    public static void main(String[] args) {
        int[] nums = new int[] {1,3,-1,-3,5,3,6,7};
        int k = 3;
        int result = priorityQueue(nums, k);
        System.out.println(result);
    }

    private static int slideWindow(int[] nums,int k) {
        int maxVal = Integer.MIN_VALUE;
        int startIndex = 0,i=0,sum=0;
        while (i<nums.length) {
            // 窗口小于给定长度 向后滑动右侧窗口 累计求和
            while (i-startIndex<k) {
                sum+=nums[i];
                i++;
            }
            // 比较并得到最大值
            maxVal = Math.max(sum,maxVal);
            // 左边窗口向前滑动
            sum-=nums[startIndex];
            startIndex++;
        }
        return maxVal;
    }

    private static int priorityQueue(int[] nums,int k) {
        int maxVal = Integer.MIN_VALUE;
        int sum = 0;

        // 创建一个优先级队列，用于存储元素和它们的索引
        PriorityQueue<int[]> queue = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
        Arrays.stream(nums)
                .forEach(val -> queue.add(new int[]{val}));
        while (!queue.isEmpty()) {
            Arrays.stream(queue.poll()).findFirst()
                    .ifPresent(System.out::println);
        }
        return 0;
    }
}
